/**  
* @Title: ServiceRecordServiceImpl.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年6月10日  
*/
package com.fortune.rainshine.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fortune.rainshine.common.PageResult;
import com.fortune.rainshine.service.service.ShopService;
import com.fortune.rainshine.system.dao.RoleMapper;
import com.fortune.rainshine.system.service.ServiceRecordService;
import com.fortune.rainshinecommon.model.ShopModel;
import com.fortune.rainshinecommon.model.system.Role;
import com.fortune.rainshinecommon.model.system.User;

/**  
* @ClassName: ServiceRecordServiceImpl  
* @Description: TODO(这里用一句话描述这个类的作用)  
* @author 张鑫
* @phone 17561783855
* @date 2019年6月10日  
*    
*/
@Service
public class ServiceRecordServiceImpl implements ServiceRecordService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private ShopService shopService;
	

	@Override
	public PageResult<ShopModel> list(String column, String value,User user) {
		List<ShopModel> shopTotal = new ArrayList<>();
		// 获取数据，默认按创建时间倒序排列
		List<ShopModel> shopList;

		List<Role> role = roleMapper.selectByUserId(user.getUserId());
		Integer roleId = role.get(0).getRoleId();
		// 参数封装
		Map<String, Object> map = new HashMap<>();

		// 判断为boss
		if (roleId == 1 || roleId == 2) {
			//获取总店数据
			shopList = shopService.getShopByShopLevel(1);
			
			if (!shopList.isEmpty()) {
				shopList.get(0).setParentId(-1);
				shopTotal.addAll(shopList);
				
				//获取所有招商
				List<ShopModel> shop2List = shopService.getShopByShopLevel(2);
				if (!shop2List.isEmpty()) {
					for (ShopModel one : shop2List) {
						one.setParentId(shopList.get(0).getId());
						List<ShopModel> shop3List = shopService.getShopByMerchantsId(one.getManagerId());
						
						if (!shop3List.isEmpty()) {
							for (ShopModel two : shop3List) {
								two.setParentId(one.getId());
							}
							shopTotal.addAll(shop3List);
						}
					}
					shopTotal.addAll(shop2List);
					
				}
			}
		}
		// 判断为招商
		else if (roleId == 4) {
			// 获取招商门店数据
			ShopModel shop4 = shopService.getShopByShopId(user.getShopId());
			shop4.setParentId(-1);
			shopTotal.add(shop4);
			List<ShopModel>  shop5List = shopService.getShopByMerchantsId(shop4.getManagerId());
			
			if (!shop5List.isEmpty()) {
				for (ShopModel item : shop5List) {
					item.setParentId(shop4.getId());
				}
				shopTotal.addAll(shop5List);
			}
		}
		// 判断为店长
		else if (roleId == 5) {
			// 获取招商门店数据
		    ShopModel shop5 = shopService.getShopByShopId(user.getShopId());
		    shop5.setParentId(-1);
			shopTotal.add(shop5);

		}
		return new PageResult<>(9999, shopTotal);
	}
	
}
